#!/usr/bin/env node

const log=console.log,
    {con}=require('./common.js');

async function getSpots(){
    const sql='select * from city where id<10 order by rand() limit 5;';
    let [rows]=await con.execute(sql);
    return rows
}

async function getSpotById(id){
    const sql=`select * from spot where city=${id};`;
    let [rows]=await con.execute(sql);
    return rows
}

async function getSpotByIdDetail(id){
    const sql=`select * from spot where id=${id};`;
    let [rows]=await con.execute(sql);
    return rows[0]
}

async function getSpotByName(name){
    const sql1=`select id,name from spot where name like'%${name}%';`; 
    let [rows1]=await con.execute(sql1);
    const sql2=`select spot.id,spot.name from spot,city where city.name like '%${name}%' and city.id=spot.city;`
    let [rows2]=await con.execute(sql2);
    rows1=rows1.concat(rows2)
    log(rows1)
    return rows1
}

module.exports={getSpots,getSpotById,getSpotByIdDetail,getSpotByName}